package com.grad.why.studentsystem.controller;

import com.alibaba.dubbo.common.json.JSON;
import com.grad.why.studentsystem.domain.Classinfo;
import com.grad.why.studentsystem.domain.Runtimecoursearrangeinfodetail;
import com.grad.why.studentsystem.domain.Runtimecoursearrangeinforecord;
import com.grad.why.studentsystem.domain.current.ClassArriveInfoNameAndMax;
import com.grad.why.studentsystem.domain.current.CurrentClassArriveInfos;
import com.grad.why.studentsystem.domain.current.CurrentCourseDistributeInfos;
import com.grad.why.studentsystem.domain.current.PersonArriveInfoNameAndMax;
import com.grad.why.studentsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.ClassInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/studentManageSystem")
public class GraghicController {

    @Autowired
    private RuntimeCourseArrangeInfoRecordService runtimeCourseArrangeInfoRecordService;

    @Autowired
    private ClassinfoService classinfoService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentarrivedinfoService studentarrivedinfoService;

    @Autowired
    private RuntimeCourseArrangeInfoDetailService runtimeCourseArrangeInfoDetailService;

    @RequestMapping("/getAllStudentArrivedRate")
    @ResponseBody
    public double getAllStudentArrivedRate(){
        try{
            List<Runtimecoursearrangeinforecord> runtimecoursearrangeinforecords = runtimeCourseArrangeInfoRecordService.getRuntimeCourseArrangeInfoRecords();
            float realCount = 0;
            float shouldCount = 0;
            for(int i = 0;i<runtimecoursearrangeinforecords.size();i++){
                Runtimecoursearrangeinforecord runtimecoursearrangeinforecord = runtimecoursearrangeinforecords.get(i);
                realCount += runtimecoursearrangeinforecord.getRealcount();
                shouldCount += runtimecoursearrangeinforecord.getShouldcount();
            }
            if(shouldCount == 0){
                return 100;
            }
            return realCount/shouldCount * 100;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 100;
    }

    @RequestMapping("/AllStudentDistributeInfo")
    @ResponseBody
    public String[] AllStudentDistributeInfo(){
        try{
            String[] infos = new String[2];

            List<Classinfo> classInfos = classinfoService.getClassInfos();
            int sum = 0;
            String classnames = "";
            String studentCount = "";

            for(int i = 0;i<classInfos.size();i++){
                Classinfo c = classInfos.get(i);
                classnames = classnames + "'"+c.getClassname() + "',";
                studentCount = studentCount + c.getStudentcount() + ",";
                sum += c.getStudentcount();
            }

            classnames = classnames + "'总学生数'";
            studentCount = studentCount + sum;
            infos[0] = "["+classnames+"]";
            infos[1] = "["+studentCount+"]";
            return infos;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/AllCourseDistrubuteInfo")
    @ResponseBody
    public String[] AllCourseDistrubuteInfo(){
        try{
            String[] courseInfos = new String[2];
            List<CurrentCourseDistributeInfos> currentCourseDistributeInfos = courseService.AllCourseDistrubuteInfo();
            String currentCourseType = "";
            String currentCourseCount = "";
            for(int i = 0;i<currentCourseDistributeInfos.size();i++){
                CurrentCourseDistributeInfos c = currentCourseDistributeInfos.get(i);
                currentCourseType = currentCourseType + "'"+c.getCourseType()+"',";
                currentCourseCount = currentCourseCount + c.getCourseCount()+",";
            }
            currentCourseType = currentCourseType.substring(0,currentCourseType.length() - 1);
            currentCourseCount = currentCourseCount.substring(0,currentCourseCount.length() - 1);
            courseInfos[0] = "["+currentCourseType+"]";
            courseInfos[1] = "["+currentCourseCount+"]";
            return courseInfos;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getAllClassStudentArrivedInfo/{classId}")
    @ResponseBody
    public String[] getAllClassStudentArrivedInfo(@PathVariable("classId") String classId){
        try{
            String[] infos = new String[2];
            Integer classid = Integer.parseInt(classId);
            List<PersonArriveInfoNameAndMax> personArriveInfoNameAndMaxes = classinfoService.getAllClassStudentArrivedInfo(classid);
            for(PersonArriveInfoNameAndMax p : personArriveInfoNameAndMaxes){
                p.setMax(1.0f);
            }

            String nameAndMax = JSON.json(personArriveInfoNameAndMaxes);
            String values = "";
            List<Float> getValues = classinfoService.getAllClassStudentArrivedInfoValues(classid);
            for(Float f : getValues){
                values = values + f +",";
            }
            nameAndMax = nameAndMax.replaceAll("\"name\"","name").replaceAll("\"max\"","max").replaceAll("\"","'");

            if(nameAndMax.equals("[]")){
                infos[0] = "[{name:'该班级暂无学生信息',max:1.0}]";
                infos[1] = "[1.0]";
            }else{
                infos[0] = nameAndMax;
                infos[1] = "["+values+"]";
            }

            return infos;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getinitAllClassArriveInfo")
    @ResponseBody
    public String[] getinitAllClassArriveInfo(){
        try{
            String[] infos = new String[2];

            List<CurrentClassArriveInfos> currentClassArriveInfos = studentarrivedinfoService.getAllCurrentClassArriveInfos();
            List<ClassArriveInfoNameAndMax> classArriveInfoNameAndMaxes = new ArrayList<>();
            for(CurrentClassArriveInfos c : currentClassArriveInfos){
                ClassArriveInfoNameAndMax classArriveInfoNameAndMax = new ClassArriveInfoNameAndMax();
                classArriveInfoNameAndMax.setName(c.getClassname());
                classArriveInfoNameAndMax.setMax(100.0f);
                classArriveInfoNameAndMaxes.add(classArriveInfoNameAndMax);
            }
            String nameAndMax = JSON.json(classArriveInfoNameAndMaxes);
            String values = "";
            for(CurrentClassArriveInfos c : currentClassArriveInfos){
                values = values + c.getRate() +",";
            }
            nameAndMax = nameAndMax.replaceAll("\"name\"","name").replaceAll("\"max\"","max").replaceAll("\"","'");
            if(nameAndMax.equals("[]")){
                infos[0] = "[{name:'暂时无班级信息',max:100.0}]";
                infos[1] = "[100.0]";
            }else{
                infos[0] = nameAndMax;
                infos[1] = "["+values+"]";
            }

            return infos;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getAllRuntimeStudentArriveCount")
    @ResponseBody
    public String getAllRuntimeStudentArriveCount(){
        try{
            List<Runtimecoursearrangeinfodetail> runtimecoursearrangeinfodetails =  runtimeCourseArrangeInfoDetailService.getAllDetails();
            if(runtimecoursearrangeinfodetails.size() == 0){
                return "[[100,'当前没有课程正在进行']]";
            }else{
                String values = "[";
                for(Runtimecoursearrangeinfodetail runtimecoursearrangeinfodetail : runtimecoursearrangeinfodetails){
                    values += runtimecoursearrangeinfodetail.getRealcount()+",";
                    values += "'"+runtimecoursearrangeinfodetail.getCoursename()+"_"+runtimecoursearrangeinfodetail.getTeachername()+"_"+runtimecoursearrangeinfodetail.getClassroomname()+"'],";
                }
                values = "[" + values +"]";
                return values;
            }
        }catch (Exception E){
            E.printStackTrace();
        }
        return "[[100,'当前没有课程正在进行']]";
    }
}
